package test;

import com.ge.med.terra.jami.platform.dm.*;
import com.ge.med.terra.jami.*;
import com.ge.med.terra.jami.image.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferUShort;
import java.util.Properties;

import javax.swing.*;

/**
 * A simple demonstration of using the image viewport.
 */
public class SimpleImageDemo extends JPanel {
//    private XpImageViewport iv    = new XpImageViewport("com.ge.med.terra.jami.render.XpPZWRenderer");
    private XpImageViewport iv    = new XpImageViewport();
    private XpMedicalImage[] imgs = null;

    public SimpleImageDemo(String[] params) {
        this.setLayout(new BorderLayout());

        iv.setPreferredSize(new Dimension(512,512));
        // Load the images and associate them with the image viewport.
        add(iv);

        if (params != null) {
            XpDMSessionProvider dmsp = new XpDMSessionProvider(params);
            dmsp.printESInfo();
            imgs = dmsp.getImages();
        } 
        System.err.println(">>>> # of frames = "+imgs.length);
        
        BufferedImage bimg = new BufferedImage(imgs[0].getSlice().width,imgs[0].getSlice().height,BufferedImage.TYPE_USHORT_GRAY);
        BufferedImage img  = (BufferedImage ) imgs[0].getPixelData();
        short[] idatasrc = ((DataBufferUShort ) img.getRaster().getDataBuffer()).getData();
        short[] idata = ((DataBufferUShort ) bimg.getRaster().getDataBuffer()).getData();
        System.arraycopy(idatasrc, 0, idata, 0, idata.length);
        
        double slope     = imgs[0].getRescaleSlope();
        double intercept = imgs[0].getRescaleIntercept();
                
        System.err.println(">>>> slope="+slope+"  intercept="+intercept);
        Properties p = new Properties();
        setProperty(p,""+0x0028+","+0x0004, getValue(0x28,0x103,imgs[0])); // pixel representation signed.
        setProperty(p,""+0x0028+","+0x0103, getValue(0x28,0x4,imgs[0])); // PHOTOMETRIC interpretation.
        setProperty(p,""+0x0028+","+0x0100, getValue(0x28,0x100,imgs[0])); // bits allocated.
        setProperty(p,""+0x0028+","+0x0101, getValue(0x28,0x101,imgs[0])); // bits stored.
        XpBufferedImage mimg = new XpBufferedImage(bimg,intercept,slope,imgs[0].getDefaultWindowWidth(),imgs[0].getDefaultWindowLevel());
        mimg.setPropertyTable(p);
        iv.setImages(new XpMedicalImage[] {mimg});
    }
    
    private void setProperty(Properties p, String key, Object value) {
    	if (value!=null) {
    		p.setProperty(key, value.toString());
    	}
    }
    
    private Object getValue(int group, int element, XpDicomObject dobj) {
        XpDicomElement de = new XpDicomElement(group,element);  
        int f = dobj.getValue(de);
        if (f==0 && de.value!=null) {
        	if (!de.value.toString().trim().equals("null")) {
        	    return de.value;
        	}
        }
        return null;
    }

    public static void main(String[] args) {
        JFrame frame       = new JFrame("Simple Image Demo");

        String[] params    = null;
        if (args.length > 0) params = new String[] {"file",args[0]};
        else params = new String[] {"file", DemoUtils.demoChooseDirectory("SimpleImageDemo")};

        SimpleImageDemo sid = new SimpleImageDemo(params);
        frame.setLocation(100,100);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setContentPane(sid);
        frame.pack();
        frame.setVisible(true);
    }
}

